<?php

    /**
    * This is the model class for table "video".
    *
    * The followings are the available columns in table 'video':
    * @property string $id
    * @property string $name
    * @property string $alias
    * @property string $youtube
    * @property string $view
    * @property string $fb_like_count
    * @property string $fb_comment_count
    * @property string $like_count
    * @property string $dislike_count
    * @property string $created
    * @property string $user_id
    * @property string $status
    *
    * The followings are the available model relations:
    * @property User $user
    * @property VideoLike[] $videoLikes
    */
    class Video extends CActiveRecord
    {
        /**
        * Returns the static model of the specified AR class.
        * @param string $className active record class name.
        * @return Video the static model class
        */
        public static function model($className=__CLASS__)
        {
            return parent::model($className);
        }

        /**
        * @return string the associated database table name
        */
        public function tableName()
        {
            return 'video';
        }

        /**
        * @return array validation rules for model attributes.
        */
        public function rules()
        {
            // NOTE: you should only define rules for those attributes that
            // will receive user inputs.
            return array(
                array('name, alias, youtube, user_id, status', 'required'),
                array('name, alias, youtube', 'length', 'max'=>255),
                array('view, fb_like_count, fb_comment_count, user_id', 'length', 'max'=>10),
                array('status', 'length', 'max'=>8),
                // The following rule is used by search().
                // Please remove those attributes that should not be searched.
                array('id, name, alias, youtube, view, fb_like_count, fb_comment_count, like_count, dislike_count, created, user_id, status', 'safe', 'on'=>'search'),
            );
        }

        /**
        * @return array relational rules.
        */
        public function relations()
        {
            // NOTE: you may need to adjust the relation name and the related
            // class name for the relations automatically generated below.
            return array(
                'user' => array(self::BELONGS_TO, 'User', 'user_id'),
                'videoLikes' => array(self::HAS_MANY, 'VideoLike', 'video_id'),
            );
        }

        /**
        * @return array customized attribute labels (name=>label)
        */
        public function attributeLabels()
        {
            return array(
                'id' => 'ID',
                'name' => 'Name',
                'alias' => 'Alias',
                'youtube' => 'Youtube',
                'view' => 'View',
                'fb_like_count' => 'Fb Like Count',
                'fb_comment_count' => 'Fb Comment Count',
                'like_count' => 'Like Count',
                'dislike_count' => 'Dislike Count',
                'created' => 'Created',
                'user_id' => 'User',
                'status' => 'Status',
            );
        }

        /**
        * Retrieves a list of models based on the current search/filter conditions.
        * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
        */
        public function search()
        {
            // Warning: Please modify the following code to remove attributes that
            // should not be searched.

            $criteria=new CDbCriteria;

            $criteria->compare('id',$this->id,true);
            $criteria->compare('name',$this->name,true);
            $criteria->compare('alias',$this->alias,true);
            $criteria->compare('youtube',$this->youtube,true);
            $criteria->compare('view',$this->view,true);
            $criteria->compare('fb_like_count',$this->fb_like_count,true);
            $criteria->compare('fb_comment_count',$this->fb_comment_count,true);
            $criteria->compare('like_count',$this->like_count,true);
            $criteria->compare('dislike_count',$this->dislike_count,true);
            $criteria->compare('created',$this->created,true);
            $criteria->compare('user_id',$this->user_id,true);
            $criteria->compare('status',$this->status,true);

            return new CActiveDataProvider($this, array(
                'criteria'=>$criteria,
            ));
        }

        public function getStatusData(){
            return array(
                'APPROVED' => Yii::t('app', 'Approved'),
                'PENDING' => Yii::t('app', 'Pending'),
                'DISABLE' => Yii::t('app', 'Disable'),
            );
        }

        public function getIsApproved(){
            return $this->status == "APPROVED";
        }
        public function getIsPending(){
            return $this->status == "PENDING";
        }
        public function getIsDisable(){
            return $this->status == "DISABLE";
        }


        public function getUrl(){
            return 'http://'.Yii::app()->params->domain.Yii::app()->createUrl('/hehe/web/video/view', array('id' => $this->id)); 
        }
        public function getUrlUpdate(){
            return 'http://'.Yii::app()->params->domain.Yii::app()->createUrl('/hehe/web/video/update', array('id' => $this->id)); 
        } 
        
        public function getImageUrl(){
           
            return "http://img.youtube.com/vi/{$this->youtube}/hqdefault.jpg";
        }

        public function getImageThumbUrl(){
            return "http://img.youtube.com/vi/{$this->youtube}/mqdefault.jpg";
        }

        
        public function getVote_count(){
            return $this->like_count + $this->dislike_count;
        } 
        public function getLike_count_percent(){
            return $this->vote_count ? ($this->like_count * 100 / $this->vote_count) : 0;
        }
        public function getDislike_count_percent(){
            return $this->vote_count ? ($this->dislike_count * 100 / $this->vote_count) : 0;
        }
        public function getNameEncode(){
            return CHtml::encode($this->name);
        }

        public function getCreatedTime(){
            $created = $this->reviewed ? $this->reviewed : $this->created;
            $format = date('G', strtotime($this->created)) ? 'G:i d/m/Y' : 'd/m/Y';
            return date($format, strtotime($created));
            //return MyDateTime::relative($this->created);
        }
        
        public function getUserName(){
            return $this->user ? $this->user->name : null;
        }
        
        private $_newer;
        public function getNewer(){
            if($this->_newer) return $this->_newer;
            return $this->_newer = $this->find(new CDbCriteria(array(
                'condition' => "t.id > {$this->id} AND t.`status` = 'APPROVED'",
                'order' => 't.id ASC',
            )));
        }

        private $_older;
        public function getOlder(){
            if($this->_older) return $this->_older;
            return $this->_older = $this->find(new CDbCriteria(array(
                'condition' => "t.id < {$this->id} AND t.`status` = 'APPROVED'",
                'order' => 't.id DESC',
            )));
        }

        public function getIsAdminOwner(){
            if(Yii::app()->user->isGuest) return FALSE;
            if(Yii::app()->user->userInfo->isAdmin) return TRUE;
            $image = Image::model()->findByAttributes(array(
                'id' => $this->id,
                'user_id' => Yii::app()->user->id,
            ));
            return $image ? TRUE : FALSE;
        }
        public function getIsModOwner(){
            if(Yii::app()->user->isGuest) return FALSE;
            if(Yii::app()->user->userInfo->isMod) return TRUE;
            $image = Image::model()->findByAttributes(array(
                'id' => $this->id,
                'user_id' => Yii::app()->user->id,
            ));
            return $image ? TRUE : FALSE;
        }
        


}